<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item v-for="(item,index) in col" :key="index" :label="item.title" :prop="item.prop">
        <el-input v-if="item.type==='input'" v-model="queryParams[item.prop]" :placeholder="item.placeholder" clearable size="mini" style="width: 240px" @keyup.enter.native="handleQuery" />
        <el-select v-if="item.type==='select'" v-model="queryParams[item.prop]" :placeholder="item.placeholder" clearable size="mini" style="width: 240px">
          <el-option v-for="dict in item.options" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
        <el-date-picker v-if="item.type==='daterange'" v-model="queryParams[item.prop]" size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleCommand('handleAdd')" v-hasPermi="['project:projects:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleCommand('handleUpdate')" v-hasPermi="['project:projects:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['project:projects:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport" v-hasPermi="['project:projects:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table size="small" v-loading="loading" :data="projectsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <!-- <el-table-column label="项目ID" align="center" prop="id" width="60" /> -->
      <el-table-column label="项目名称" align="center" prop="pName" />
      <el-table-column label="施工单位" align="center" prop="pSgdw" />
      <el-table-column label="项目状态" align="center" prop="pStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pm_status_type" :value="scope.row.pStatus" />
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="施工单位负责人" align="center" prop="pXmjl" />
      <el-table-column label="施工单位负责人联系方式" align="center" prop="pLxfs" width="200" />
      <el-table-column label="费用" align="center" prop="pMoney" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleCommand('handleUpdate',scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleCommand('handleDelete',scope.row)" v-if="scope.row.pStatus ==='2' || scope.row.pStatus ==='3' || scope.row.pStatus ==='4'">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-setting" @click="handleCommand('handleLaunch',scope.row)" v-if="scope.row.pStatus ==='2'">启动</el-button>
          <el-button size="mini" type="text" icon="el-icon-s-data" @click="handleCommand('toProject',scope.row)" v-if=" scope.row.pStatus !=='5' && scope.row.pStatus !=='2' ">结项</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <!-- 结项弹出框 -->
    <PostProject ref="postproject" />
    <!-- 新增弹出框 -->
    <AddProject ref="addproject" />
    <!-- 启动弹窗 -->
    <Launch ref="launch" />
  </div>
</template>

<script>
import { listProjects, getProjects, delProjects, addProjects, updateProjects, exportProjects } from "@/api/business/project/projects";
import PostProject from '../../components/post_project'
import AddProject from './handle/add'
import Launch from './handle/launch.vue'
export default {
  name: "Projects",
  dicts: ['pm_status_type'],
  components: { PostProject, AddProject, Launch },
  data() {
    return {
      col: [{ title: '项目名称', prop: 'pName', type: 'input', placeholder: '请输入项目名称', options: [], show: true, disabled: false },
      // { title: '施工单位', prop: 'name', type: 'input', placeholder: '请选择', options: [], show: true, disabled: false },
      // { title: '项目经理', prop: 'name', type: 'input', placeholder: '请选择', options: [], show: true, disabled: false },
      { title: '项目状态', prop: 'pStatus', type: 'select', placeholder: '请选择项目状态', options: [], show: true, disabled: false },
        // { title: '计划启动日期', prop: 'daterange', type: 'daterange', placeholder: '请选择', options: [], show: true, disabled: false },
        // { title: '计划结项日期', prop: 'daterange', type: 'daterange', placeholder: '请选择', options: [], show: true, disabled: false }
      ],
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 工程管理系统-工程项目信息表格数据
      projectsList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pName: null,
        pStatus: null,
      },
      // 总条数
      total: 0
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getoptions()
  },
  methods: {
    /**获取对应字典值 */
    getoptions() {
      this.col.forEach(element => {
        element.prop === 'pStatus' ? element.options = this.dict.type.pm_status_type : []
      });
    },
    /** 查询工程管理系统-工程项目信息列表 */
    getList() {
      this.loading = true;
      listProjects(this.queryParams).then(response => {
        this.projectsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工程管理系统-工程项目信息编号为"' + ids + '"的数据项？').then(function () {
        return delProjects(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有工程管理系统-工程项目信息数据项？').then(() => {
        this.exportLoading = true;
        return exportProjects(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => { });
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case 'toProject':
          this.$refs.postproject.init(row)
          break;
        case "handleAdd":
          this.$refs.addproject.init()
          break;
        case "handleLaunch":
          this.$refs.launch.init(row)
          break;
        case "handleUpdate":
          this.$refs.addproject.init(!row ? { id: this.ids[0] } : row)
          break;
        case "handleDelete":
          this.handleDelete(row)
          break;
        default:
          break;
      }
    },
  }
};
</script>